package MyFramePro.Utils;

import MyFramePro.Constants.SystemConstants;
import MyFramePro.Domain.Entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Objects;

/**
 * 这里是安全框架的工具类
 */
public class SecurityUtils {
    /**
     * 获取用户认证信息
     **/
    public static LoginUser getLoginUser() {

        return (LoginUser) getAuthentication().getPrincipal();
    }

    /**
     * 获取当前 Authentication
     */
    public static Authentication getAuthentication() {

        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 判断当前用户角色是否为 管理员
     */
    public static Boolean isAdmin(){
        Long id = getLoginUser().getUser().getId();
        return id != null && 1L == id;
    }


    /**
     *  获取当前认证用户的用户 ID
     */
    public static Long getUserId() {

        return getLoginUser().getUser().getId();
    }

}